<template>
	<view class="uni-container">
		<uni-forms ref="form" :model="formData" validate-trigger="submit" err-show-type="toast">

			<uni-forms-item name="category_id" label="文章分类" label-width="80px">
				<uni-data-checkbox v-model="formData.category_id" collection="opendb-news-categories"
					field="_id as value, name as text"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="title" label="标题" required label-width="80px">
				<uni-easyinput placeholder="标题" v-model="formData.title" trim="both"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="images" label="图片" label-width="80px">
				<uni-file-picker v-model="formData.images" fileMediatype="image" mode="grid" @select="select" @success="success" @fail="fail"/>
			</uni-forms-item>
			<uni-forms-item name="content" label="文章内容" required label-width="80px">
				<uni-easyinput placeholder="文章内容" v-model="formData.content" trim="right" autoHeight
					type="textarea"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item name="mall_category_id" label="标签">
				<uni-data-checkbox :multiple="true" v-model="formData.mall_category_id" max="3" min="1"
					collection="opendb-mall-categories" field="name as text, _id as value"></uni-data-checkbox>
			</uni-forms-item>
			<uni-forms-item name="mode" label="排版显示模式" label-width="80px">
				<uni-data-picker v-model="formData.mode" :localdata="formOptions.mode"></uni-data-picker>
			</uni-forms-item>

			<view class="uni-button-group">
				<button type="primary" class="uni-button" @click="submit">提交</button>
			</view>
		</uni-forms>
	</view>
</template>

<script>
	import {
		validator
	} from '../../js_sdk/validator/opendb-news-articles.js';

	const db = uniCloud.database();
	const dbCollectionName = 'opendb-news-articles';

	function getValidator(fields) {
		let result = {}
		for (let key in validator) {
			if (fields.indexOf(key) > -1) {
				result[key] = validator[key]
			}
		}
		return result
	}



	export default {
		data() {
			let formData = {
				"user_id": "",
				"category_id": "666ad93f8b0da4a4e4e15b6e",
				"title": "",
				"content": "",
				"mall_category_id": [],
				"images": [],
				"mode": "0"
			}
			return {
				formData,
				formOptions: {
					mode: [{
						value: 0,
						text: "左图右文"
					}]
				},
				rules: {
					...getValidator(Object.keys(formData))
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			
			select(e){
				console.log(e)
			},
			success(e){
				console.log(e)
				console.log(this.formData)
			},
			fail(e){
				console.log(e)
			},

			/**
			 * 验证表单并提交
			 */
			submit() {
				uni.showLoading({
					mask: true
				})
				this.$refs.form.validate().then((res) => {
					return this.submitForm(res)
				}).catch(() => {}).finally(() => {
					uni.hideLoading()
				})
			},

			/**
			 * 提交表单
			 */
			submitForm(value) {
				// 使用 clientDB 提交数据
				return db.collection(dbCollectionName).add(value).then((res) => {
					uni.showToast({
						icon: 'none',
						title: '新增成功'
					})
					this.getOpenerEventChannel().emit('refreshData')
					setTimeout(() => uni.navigateBack(), 500)
				}).catch((err) => {
					uni.showModal({
						content: err.message || '请求服务失败',
						showCancel: false
					})
				})
			}
		}
	}
</script>

<style>
	.uni-container {
		padding: 15px;
	}

	.uni-input-border,
	.uni-textarea-border {
		width: 100%;
		font-size: 14px;
		color: #666;
		border: 1px #e5e5e5 solid;
		border-radius: 5px;
		box-sizing: border-box;
	}

	.uni-input-border {
		padding: 0 10px;
		height: 35px;

	}

	.uni-textarea-border {
		padding: 10px;
		height: 80px;
	}

	.uni-button-group {
		margin-top: 50px;
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		justify-content: center;
	}

	.uni-button {
		width: 184px;
	}
</style>